package com.hkts.beltweigher.master.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.beltweigher.master.entity.ScaleData;
import com.hkts.beltweigher.master.mapper.ScaleDataMapper;
import com.hkts.beltweigher.master.service.IScaleDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.beltweigher.master.vo.PageVo;
import com.hkts.beltweigher.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2024-04-10
 */
@Service
@Transactional
public class ScaleDataServiceImpl extends ServiceImpl<ScaleDataMapper, ScaleData> implements IScaleDataService {


    @Autowired
    private ScaleDataMapper scaleDataMapper;

    @Override
    public IPage<ScaleData> selectPage(Page<ScaleData> page, ScaleData scaleData) {
        QueryWrapper<ScaleData> query = Wrappers.query();
        return scaleDataMapper.selectPage(page, query);
    }


    @Override
    public List<ScaleData> selectList(ScaleData scaleData) {
        QueryWrapper<ScaleData> query = Wrappers.query();
        return scaleDataMapper.selectList(query);
    }

    @Override
    public ScaleData selectScaleData() {
        QueryWrapper<ScaleData> query = Wrappers.query();
        query.lambda().last("LIMIT 1").orderByDesc(ScaleData::getCreateTime);
        ScaleData scaleData = scaleDataMapper.selectOne(query);
        return scaleData;
    }

    @Override
    public Map<String, Object> getChar(Page<ScaleData> page, String startTime, String endTime) throws ParseException {

        QueryWrapper<ScaleData> query = Wrappers.query();
        query.lambda().ge(ScaleData::getCreateTime, startTime)
                .le(ScaleData::getCreateTime, endTime);
        Page<ScaleData> scaleDataPage = scaleDataMapper.selectPage(page, query);
        Integer integer = scaleDataMapper.selectCount(query);
        List<ScaleData> records = scaleDataPage.getRecords();
        Map<String, Object> result = new HashMap<String, Object>();
        if (records.size() > 0) {
            List<Float> strListZw1 = new ArrayList<>();
            List<Float> strListZw2 = new ArrayList<>();
            List<Float> strListZw3 = new ArrayList<>();
            List<Float> strListZw4 = new ArrayList<>();
            List<Float> strListZw5 = new ArrayList<>();
            List<String> times = new ArrayList<>();
            for (int i = 0; i < records.size(); i++) {

                strListZw1.add(records.get(i).getB1());
                strListZw2.add(records.get(i).getB2());
                strListZw3.add(records.get(i).getB3());
                strListZw4.add(records.get(i).getB4());
                strListZw5.add(records.get(i).getB5());
                String strDateHH = DateUtil.getStrDateHH(records.get(i).getCreateTime());
                times.add(strDateHH);
            }
            Map<String, Object> series = new HashMap<>();
            series.put("中文仪表1", strListZw1);
            series.put("中文仪表2", strListZw2);
            series.put("中文仪表3", strListZw3);
            series.put("中文仪表4", strListZw4);
            series.put("中文仪表5", strListZw5);
            List<String> collect = series.keySet().stream().sorted().collect(Collectors.toList());
            TreeMap<String, Object> stringObjectTreeMap = new TreeMap<>(series);

            result.put("series", stringObjectTreeMap);
            result.put("times", times);
            result.put("list", collect);
            result.put("totalRows", integer);
            result.put("pageSize", page.getSize());
            result.put("pageNumber", page.getCurrent());
            return result;
        } else {
            result.put("series", new ArrayList<>());
            result.put("list", new ArrayList<>());
            result.put("totalRows", integer);
            result.put("pageSize", page.getSize());
            result.put("pageNumber", page.getCurrent());
            return result;
        }
    }


}
